package com.milaidoo.fudong;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

/**
 * TODO
 * @author cuiran
 * @version 1.0.0
 */
public class VerticalScrollTextView extends AppCompatTextView {
    public boolean isStarting = true;//是否开始滚动
    private float step = 0f;
    private Paint mPaint= getPaint(); //绘图样式
    private String text;
    private float width;
    private List<String> textList = new ArrayList<String>();    //分行保存textview的显示信息。
    private int textSize = 40;
    private int textColor = Color.BLACK;

    public void init(WindowManager windowManager)
    {

    }
    public VerticalScrollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public VerticalScrollTextView(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        text=getText().toString();
        mPaint = getPaint();
        mPaint.setColor(textColor);
        mPaint.setTextSize(textSize);
        if(text==null|text.length()==0){
            return ;
        }
        //下面的代码是根据宽度和字体大小，来计算textview显示的行数。
        setText(text,textSize,textColor);
        Log.e("textviewscroll",""+textList.size());
    }
    public void setText(String text,int textSize,int textColor){
        textList.clear();
        this.text = text;
        this.textColor = textColor;
        this.textSize = textSize;
        setText(text);
        mPaint = getPaint();
        mPaint.setColor(textColor);
        mPaint.setTextSize(textSize);
        StringBuilder builder =null;
        for(int i=0;i<text.length();i++){
            if(i%12==0){
                builder = new StringBuilder();
            }
            if(i%12<=11){
                builder.append(text.charAt(i));
                if(i == text.length() - 1){
                    textList.add(builder.toString());
                }
            }
            if(i%12==11){
                textList.add(builder.toString());
            }
        }
        startScroll();
    }
    public void startScroll()
    {
        isStarting = true;
        invalidate();
    }


    public void stopScroll()
    {
        isStarting = false;
        invalidate();
    }
    //下面代码是利用上面计算的显示行数，将文字画在画布上，实时更新。
    @Override
    public void onDraw(Canvas canvas) {
        if(textList.size()==0)  return;
        for(int i = 0; i < textList.size(); i++) {
            canvas.drawText(textList.get(i), 0, this.getHeight()+(i+1)*mPaint.getTextSize()-step+30, mPaint);
        }
        invalidate();
        if(!isStarting) {
            return;
        }
        step = step + mScrollSpeed;
        if (step >= this.getHeight()+textList.size()*mPaint.getTextSize()) {
            step = 0;
        }
    }

    /**
     * 设置速度
     */
    private float mScrollSpeed = 0.3f;
    public void setScrollSpeed(float speed){
        mScrollSpeed = speed;
        invalidate();
    }

    public void setStep(float step){
        this.step = step;
    }

    /**
     * 获取当期那速度
     * @return
     */
    public float getStep(){
        return step;
    }

    /**
     * 获取当期那速度
     * @return
     */
    public float getScrollSpeed(){
        return mScrollSpeed;
    }

    public int getTextVColor(){
        return textColor;
    }

    public int getTextVSize(){
        return textSize;
    }

    public String getTextVText(){
        return text;
    }

}